package com.yhn.smartalbum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhn.smartalbum.annotation.AuthCheck;
import com.yhn.smartalbum.common.BaseResponse;
import com.yhn.smartalbum.common.ResultUtils;
import com.yhn.smartalbum.constant.UserConstant;
import com.yhn.smartalbum.model.dto.common.DeleteRequest;
import com.yhn.smartalbum.model.dto.req.*;
import com.yhn.smartalbum.model.dto.resp.UserLoginRespDTO;
import com.yhn.smartalbum.model.dto.resp.UserRespDTO;
import com.yhn.smartalbum.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * @author yhn
 * @version 1.0
 * @description
 * @date 2024/12/28 12:35
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody @Valid UserRegisterRequestDTO requestParam) {
        return ResultUtils.success(userService.userRegister(requestParam));
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginRespDTO> userLogin(@RequestBody @Valid UserLoginRequestDTO
                                                            requestParam, HttpServletRequest request) {
        return ResultUtils.success(userService.userLogin(requestParam, request));
    }

    @GetMapping("/get/login")
    public BaseResponse<UserLoginRespDTO> getLoginUser(HttpServletRequest request) {
        return ResultUtils.success(userService.getLoginUser(request));
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        return ResultUtils.success(userService.userLogout(request));
    }

    /**
     * 创建用户
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody @Valid UserAddRequestDTO requestParam) {
        return ResultUtils.success(userService.addUser(requestParam));
    }

    /**
     * 根据 id 获取用户（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<UserRespDTO> getUserById(long id) {
        return ResultUtils.success(userService.getUserById(id));
    }


    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody @Valid DeleteRequest requestParam) {
        return ResultUtils.success(userService.deleteUser(requestParam));
    }

    /**
     * 更新用户
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody @Valid UserUpdateRequestDTO requestParam) {
        return ResultUtils.success(userService.updateUser(requestParam));
    }

    /**
     * 分页获取用户封装列表（仅管理员）
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserRespDTO>> listUserByPage(@RequestBody UserQueryRequestDTO requestParam) {
        return ResultUtils.success(userService.listUserByPage(requestParam));
    }


}
